<?php
session_start();

// the if condition check if the user is already logged in through isset
// header is used to transfer the user to his home page if he is already logged in

if (isset($_GET['new'])) {
    $check = mysql_connect('localhost', 'root', '');
    if (!$check) {
        die('Could not connect:' . mysql_error());
    }
    mysql_select_db("mydb", $check);
    $member_id = $_GET['member_id'];
    $query3 = mysql_query("UPDATE `member` SET `activated`=TRUE WHERE m.`member_id`='$member_id'") or die(mysql_error());
}
if (isset($_POST['Submit'])) { //when submit is clicked Signin function will activate
    $check = mysql_connect('localhost', 'root', '');
    if (!$check) {
        die('Could not connect:' . mysql_error());
    }
    mysql_select_db("mydb", $check);
Signin();
}

// if the user click on submit button he will enter SignIn method
function Signin() {
// name of SignIn method
    $check = mysql_connect('localhost', 'root', '');
    if (!$check) {
        die('Could not connect:' . mysql_error());
    }
    mysql_select_db("mydb", $check);

// establish database connection
    $email = $_POST['email'];  //username that will be submit from a form
    $password = $_POST['password'];  //password that will be submit from a form


    $res = mysql_query("SELECT * FROM member Where email = '$email' AND password = '$password'", $check)  //this is used to retrive the data from our database according to what the user enters
            or die(mysql_error()); //eturns the error description of the last MySQL operation


    if (mysql_num_rows($res) == 1) {
    $get_member_id = mysql_query("SELECT m.`member_id` FROM `member`m WHERE m.`email` ='$email'") or die(mysql_error());
    $row2 = mysql_fetch_assoc($get_member_id);
    $member_id = $row2['member_id'];

    $getactivated = mysql_query("SELECT activated FROM member WHERE member_id = '$member_id'")
            or die(mysql_error());
    $get_activated = mysql_fetch_assoc($getactivated);
    $activated = $get_activated['activated'];
    if ($activated == true) {
            $row = mysql_fetch_assoc($res);
        $_SESSION['member_id'] = $row['member_id'];
        $_SESSION['email'] = $_POST['email']; // set the session variable to username in order to take it to another page
        //echo 'Username found'; //message appear to check that the username really found
        if (isset($_GET['val'])) {

            header('Location: conferenceSetup.php');
        } else {
            echo '<script type="text/javascript">',
            "top.window.location='main.php'",
            '</script>';
        }
    } else {
        echo '<script>',
        'alert("Please check the confirmation link send to your email inorder to Login");',
        '</script>';
       
    }
   
        //// if we found the username and password in the database
    

        
    } else {
        echo '<script type="text/javascript">',
        'alert("The Combination of username or password are Incorrect");',
        '</script>';
    }
}
?>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <link rel="stylesheet" type="text/css" href="style3.css"/>
    </head>
    <body>

        <form id='login' action='' target="_self" method='post' accept-charset='UTF-8' onsubmit="return valid()">
            <fieldset>

                <legend style="font-size: 12pt"><b>Sign In</b></legend>
                <table style="border: 0px;">
                    <input type='hidden' name='submitted' id='submitted' value='1'/>
                    <tr><td style="text-align: center;"><label for='username' >Email:</label></td>
                        <td style="text-align: center;"> <input type='text' name='email' id='username'  maxlength="50" /> </td></tr>
                    <tr> <td style="text-align: center;"> <label for='password' >Password:</label></td>
                        <td style="text-align: center; "> <input type='password' name='password' id='password' maxlength="50" /> </td></tr>
                </table>
                <p align="center"><input style="width: 60px;" type='submit' name='Submit' value='Login' /></p>
                <a href='resetPassword1.php'> Forgot/Reset Password? Click Here</a>

            </fieldset>
        </form>
    </body>
    <head>
        <script src="sysJavascript.js" language="javascript">
        </script>
    </head>

</html>